package com.hjm.admin.moule.view.activity.impl;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;

import com.allen.library.utils.ToastUtils;
import com.hjm.admin.moule.R;
import com.hjm.admin.moule.view.base.BaseActivity;
import com.hjm.admin.moule.viewmodel.activity.VMUseLoginInfo;
import com.hjm.admin.moule.widget.theme.ColorTextView;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {
    public static int REQUEST_CODE = 0X10;
    @BindView(R.id.til_name)
    TextInputLayout til_name;
    @BindView(R.id.til_password)
    TextInputLayout til_password;
    @BindView(R.id.tiet_username)
    EditText mEtUsername;
    @BindView(R.id.tiet_password)
    EditText mEtPassword;
    @BindView(R.id.ctv_register)
    ColorTextView mCtvRegister;
    @BindView(R.id.fab)
    FloatingActionButton mFab;
    private VMUseLoginInfo mModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mModel = new VMUseLoginInfo(this);
        setBinddingView(R.layout.activity_login, mModel);
    }

    @Override
    protected void initView() {
        super.initView();
        initThemeToolBar("用户登录");
    }

    @OnClick({R.id.ctv_register, R.id.fab})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ctv_register:
                startActivityForResult(new Intent(this, RegisterActivity.class), REQUEST_CODE);
                break;
            case R.id.fab:
                til_name.setErrorEnabled(false);
                til_password.setErrorEnabled(false);
                String username = mEtUsername.getText().toString();
                String password = mEtPassword.getText().toString();
                if (TextUtils.isEmpty(username)) {
                    showError(til_name, "用户名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    showError(til_password, "密码不能为空");
                }else{
                    if(password.length()<6||password.length()>18){
                        showError(til_password,"密码长度为6-18位");
                        return;
                    }
                }
                mModel.login(username,password);
                break;
        }
    }

    public void showError(TextInputLayout textInputLayout, String error) {
        textInputLayout.setError(error);
        textInputLayout.getEditText().setFocusable(true);
        textInputLayout.getEditText().setFocusableInTouchMode(true);
        textInputLayout.getEditText().requestFocus();
    }
}
